{% extends 'kingadmin/index.html' %}
{% load kingadmin_tags %}

{% block right-content-container %}
    <ol class="breadcrumb">
        <li><a href="{% url 'kingadmin:app_index' %}">HOME</a></li>
        <li><a href="{% url 'kingadmin:app_model_index' app_name%}">{{ app_name|upper }}</a></li>
        <li class="active">{% get_model_verbose_name admin_class %}</li>
    </ol>
    <h2 class="page-header">KingAdmin</h2>
    <div>
        <form class="navbar-form navbar-default" role="search" action="" style="margin-left: -16px;">
            <input class="form-control" type="search" name="_q" value="{{ admin_class.search_key }}"
                   placeholder="{% for s in admin_class.search_fields %} {{ s }},{% endfor %}">
            <input class="btn btn-primary" type="submit" value="Search">
            {% for k,v in admin_class.filter_condition.items %}
                <input type="hidden" name="{{ k }}" value="{{ v }}">
            {% endfor %}
            <input type="hidden" name="_o" value="{% get_current_sorted_column_index sorted_column %}">
        </form>
        <div class="row" style="margin-bottom: 5px;">
            {% if admin_class.list_display %}
                <form action="">
                    {% for fileter_column in admin_class.list_filter %}
                        {% build_filter_ele fileter_column admin_class %}
                    {% endfor %}
                    <div><input type="hidden" name="_o" value="{% get_current_sorted_column_index sorted_column %}">
                    </div>
                    <div class="col-md-2" style="margin-top: 18px;"><input type="submit" value="过滤"
                                                                           class="btn btn-success"></div>
                </form>
            {% endif %}
            <div class="col-md-2" style="margin-top: 18px;float: right;"><a
                    href="{% url 'kingadmin:table_obj_add' app_name model_name %}" class="btn btn-warning">添加</a></div>
        </div>
        <form onsubmit="return ActionCheck(this);" method="post">{% csrf_token %}
            <div class="row">
                <div class="col-lg-3">
                    <select name="action" class="form-control">
                        <option value="">------------</option>
                        {% for action in admin_class.actions %}
                            <option value="{{ action }}">{{ action }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div class="col-lg-2">
                    <input type="submit" class="btn btn-info" value="GO">
                </div>
            </div>
        </form>

        <table class="table table-bordered">
            <thead>
            <tr>
                <th><input type="checkbox" onclick="CheckAllObjs(this);"></th>
                {% if admin_class.list_display %}
                    {% for column in admin_class.list_display %}
                        <th><a href="?_o={% get_sorted_column column sorted_column forloop.counter0 %}{% render_filtered_args admin_class %}">
                            {% get_field_verbose_name admin_class column %}{% render_sorted_arrow column sorted_column %}</a>
                        </th>
                    {% endfor %}
                {% else %}
                    <th>{% get_model_name admin_class %}</th>
                {% endif %}
            </tr>
            </thead>
            <tbody>
            {% for obj in querysets %}
                <tr>
                    <td><input row-select="true" type="checkbox" value="{{ obj.id }}"></td>
                    {% build_table_row obj admin_class %}
                </tr>
            {% endfor %}
            </tbody>
        </table>
        <div class="pagination">
            {% if querysets %}
                {% render_paginator querysets admin_class sorted_column %}
            {% else %}
                <span>数据为空</span>
            {% endif %}
        </div>
    </div>
    <script>
        function ActionCheck(self) {
            var selected_action = $('select[name="action"]').val();
            var selected_objs = $("input[row-select]:checked");
            {#var selected_objs = $('input[row-select]').filter(':checked');#}
            console.log(selected_action);
            if (!selected_action) {
                alert('no action selected');
                return false
            }
            if (selected_objs.length == 0) {
                alert('no objects selected');
                return false
            } else {
                //生成一个标签，放到form里
                var ele = '<input type="hidden" >';
                var selected_ids = [];
                $.each(selected_objs, function () {
                    //console.log($(this));
                    selected_ids.push($(this).val());
                });
                console.log(selected_ids);
                var input_ele = '<input type="hidden" name="selected_ids" value=' + JSON.stringify(selected_ids) + '>';
                $(self).append(input_ele);
            }
        }

        function CheckAllObjs(self) {
            if ($(self).prop('checked')) {
                $('input[row-select]').prop('checked', true);
            } else {
                $('input[row-select]').prop('checked', false);
            }
        }
    </script>
{% endblock %}